<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Role;

class RoleController extends Controller{

    public function index(){

    }

    //查询
    public function list(Request $request){
        $page = $request->input('page');
        $limit = $request->input('limit');
        $role_name = $request->input('role_name');

        $where = [];
        if(!empty($role_name)){
            $where[] = ['role_name','like','%'.$role_name.'%'];
        }

        $role_data = Role::select('id','role_name','content','created_at')
                    ->where($where)
                    ->where('status',1)
                    ->orderBy('updated_at','desc')
                    ->paginate($limit)
                    ->toArray();

        $data = [];

        if($role_data){
            $data = [
                'code'=>200,
                'msg'=>'success',
                'data'=>$role_data
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'查询数据异常',
                'data'=>[]
            ];
        }

        return response()->json($data);

    }

    //保存
    public function save(Request $request){
        $role_name = $request->input('role_name');
        $content = $request->input('content');
        $id = $request->input('id',0);

        $now = date('Y-m-d H:i:s');

        $content = empty($content)?'这家伙很懒，什么都没有留下！':$content;

        //修改
        if($id !=0){
            $role = Role::find($id);
            $role->updated_at = $now;
        }else{
            $role = new Role();
            $role->updated_at = $now;
            $role->created_at = $now;
        } 

        $role->role_name = $role_name;
        $role->content = $content;
        

        $data = [];
        $res = $role->save();  //添加成功返回true

        if($res){
            $data = [
                'code'=>200,
                'msg'=>'保存成功'
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'保存失败'
            ];
        }
        return response()->json($data);
        


    }

    //删除
    public function del(Request $request){
        $id = $request->input('id');

        $res = Role::destroy($id);

        if($res){
            $data = [
                'code'=>200,
                'msg'=>'删除成功'
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'删除失败'
            ];
        }
        return response()->json($data);
    }



    public function getRole(){
        $role_arr = Role::select('id','role_name')->get()->toArray(); 
        if($role_arr){
            $data = [
                'code'=>200,
                'msg'=>'success',
                'data'=>$role_arr
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'查询数据异常',
                'data'=>[]
            ];
        }

        return response()->json($data);
        
    }



    //返回角色id与role_name的关联数组
    public function role_pluck(){
        $role_arr = Role::pluck('role_name','id');
        //return $role_arr;
        
        if($role_arr){
            $data = [
                'code'=>200,
                'msg'=>'success',
                'data'=>$role_arr
            ];
        }else{
            $data = [
                'code'=>100,
                'msg'=>'查询数据异常',
                'data'=>[]
            ];
        }

        return response()->json($data);
        

    }




}